package com.jyw.web.controller.system;

import com.jyw.common.annotation.Anonymous;
import com.jyw.common.constant.Constants;
import com.jyw.common.core.domain.CommonResult;
import com.jyw.system.domain.SysTeam;
import com.jyw.system.domain.SysUserTeam;
import com.jyw.system.domain.vo.DeptTeamUserTreeVo;
import com.jyw.system.service.ISysTeamService;
import com.jyw.system.service.ISysUserTeamService;
import com.jyw.system.utils.TeamUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 沈峻
 * @ClassName SysCommonController
 * @Description TODO
 * @Date 2023/9/6 15:17
 **/
@RestController
@RequestMapping("sys/common")
@Api(tags = "系统公共接口")
public class SysCommonController {

    @Resource
    private ISysUserTeamService userTeamService;

    @Resource
    private ISysTeamService teamService;

    @GetMapping("dept/team/user")
    @ApiOperation("部门小组用户联动数据")
    public CommonResult<List<DeptTeamUserTreeVo>> deptTeamUser(){
        //查询所有小组
        List<SysTeam> teamList = TeamUtils.cacheList();
        //查询所有小组的用户
        List<SysUserTeam> usetTeamList = userTeamService.lambdaQuery().eq(SysUserTeam::getIsEnable, 1).list();
        Map<Long, List<SysTeam>> collect = teamList
                .stream()
                .filter(data->!ObjectUtils.isEmpty(data.getDeptId()))
                //按部门分组
                .collect(Collectors.groupingBy(SysTeam::getDeptId));
        return CommonResult.success(collect.entrySet()
                .stream()
                .map(entry -> new DeptTeamUserTreeVo(entry, usetTeamList))
                .collect(Collectors.toList()));
    }
}
